/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkl53.view;


import java.awt.CardLayout;
import java.awt.Component;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import pkl53.controller.BackupKuesionerUsaha;
import pkl53.controller.KuesionerControllerUsaha;
import pkl53.controller.SubmitAllControllerUsaha;
import pkl53.model.B3Rinciusaha;
import pkl53.model.B3r15Pekerja;





/**
 *
 * @author HP G42 (RILLA)
 */
public class DataEntriUsahaPanel extends javax.swing.JPanel {
    

    private CardLayout card;
    private JPanel panel;
    private KuesionerControllerUsaha kuesionerControllerUsaha;
    private int jumlahPekerja;
    private int loop = 1;
    private ArrayList<B3Rinciusaha> listB3Model = new ArrayList<B3Rinciusaha>();
    private ArrayList<ArrayList<B3r15Pekerja>> listAllB3R15 = new ArrayList<ArrayList<B3r15Pekerja>>();
    private boolean lastLoop = false;
    private BackupKuesionerUsaha backupKuesionerUsaha;

    /**
     * Creates new form DataEntriUsahaPanel
     */
    public DataEntriUsahaPanel() {
        initComponents();
        kuesionerControllerUsaha = new KuesionerControllerUsaha();
        kuesionerControllerUsaha.setBlok123Usaha(blokUsaha1231);
        kuesionerControllerUsaha.setBlokUsaha4(blokUsaha41);
        kuesionerControllerUsaha.setBlokUsaha5(blokUsaha51);
        kuesionerControllerUsaha.setBlokUsaha5B(blokUsaha5B1);
        kuesionerControllerUsaha.setBlokUsaha5C(blokUsaha5C1);
        kuesionerControllerUsaha.setBlokUsaha5D(blokUsaha5D1);
        
        backupKuesionerUsaha = new BackupKuesionerUsaha();
        backupKuesionerUsaha.setBlok123Usaha(blokUsaha1231);
        backupKuesionerUsaha.setBlokUsaha4(blokUsaha41);
        backupKuesionerUsaha.setBlokUsaha5(blokUsaha51);
        backupKuesionerUsaha.setBlokUsaha5B(blokUsaha5B1);
        backupKuesionerUsaha.setBlokUsaha5C(blokUsaha5C1);
        backupKuesionerUsaha.setBlokUsaha5D(blokUsaha5D1);
        
        blokUsaha51.setBlokUsaha5b(blokUsaha5B1);
        blokUsaha41.setBlokUsaha123(blokUsaha1231);
        
        blokUsaha1231.setName("blok123");
        blokUsaha41.setName("blok4");
        blokUsaha51.setName("blok5");
        blokUsaha5B1.setName("blok5b");
        blokUsaha5C1.setName("blok5c");
        blokUsaha5D1.setName("blok5d");
        
//        Submit.setContentAreaFilled(false);
        
    }
   public  void setmainFrame(JPanel panel){
       this.panel=panel;}
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        backgroundLain1 = new pkl53.imageClass.BackgroundLain();
        jScrollPane1 = new javax.swing.JScrollPane();
        containerPanel = new javax.swing.JPanel();
        blokUsaha1231 = new pkl53.view.BlokUsaha123();
        blokUsaha41 = new pkl53.view.BlokUsaha4();
        blokUsaha51 = new pkl53.view.BlokUsaha5();
        blokUsaha5B1 = new pkl53.view.BlokUsaha5B();
        blokUsaha5C1 = new pkl53.view.BlokUsaha5C();
        blokUsaha5D1 = new pkl53.view.BlokUsaha5D();
        nextButton = new javax.swing.JButton();
        previousButton = new javax.swing.JButton();
        homeButton = new javax.swing.JButton();
        Save = new javax.swing.JButton();
        Submit = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        jScrollPane1.setOpaque(false);
        jScrollPane1.setPreferredSize(new java.awt.Dimension(1070, 1070));

        containerPanel.setBackground(new java.awt.Color(204, 255, 204));
        containerPanel.setOpaque(false);
        containerPanel.setLayout(new java.awt.CardLayout());
        containerPanel.add(blokUsaha1231, "card7");
        containerPanel.add(blokUsaha41, "card3");
        containerPanel.add(blokUsaha51, "card4");
        containerPanel.add(blokUsaha5B1, "card5");
        containerPanel.add(blokUsaha5C1, "card6");
        containerPanel.add(blokUsaha5D1, "card7");

        jScrollPane1.setViewportView(containerPanel);

        nextButton.setText("Next");
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });

        previousButton.setText("Previous");
        previousButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                previousButtonActionPerformed(evt);
            }
        });

        homeButton.setText("Home");
        homeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                homeButtonActionPerformed(evt);
            }
        });

        Save.setText("Save");
        Save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveActionPerformed(evt);
            }
        });

        Submit.setText("Submit");
        Submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SubmitActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Data Entri");

        javax.swing.GroupLayout backgroundLain1Layout = new javax.swing.GroupLayout(backgroundLain1);
        backgroundLain1.setLayout(backgroundLain1Layout);
        backgroundLain1Layout.setHorizontalGroup(
            backgroundLain1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(backgroundLain1Layout.createSequentialGroup()
                .addGap(119, 119, 119)
                .addGroup(backgroundLain1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(backgroundLain1Layout.createSequentialGroup()
                        .addComponent(Save, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Submit, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(homeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(previousButton, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nextButton, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(backgroundLain1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1088, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(166, Short.MAX_VALUE))
        );
        backgroundLain1Layout.setVerticalGroup(
            backgroundLain1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, backgroundLain1Layout.createSequentialGroup()
                .addGap(0, 56, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(backgroundLain1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nextButton)
                    .addComponent(previousButton)
                    .addComponent(homeButton)
                    .addComponent(Submit)
                    .addComponent(Save))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 623, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(backgroundLain1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(backgroundLain1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonActionPerformed
        // TODO add your handling code here:
        card = (CardLayout) getContainerPanel().getLayout();
        
        if (getCurrentCard().getName().equals("blok5")) {
           // blokUsaha5B1.setKKSArtNup(blokUsaha1231.getB1Keterangantempat1.getKks() + blokUsaha51.getB5R1NOART().getText() + blokUsaha51.getB5R2().getText());
        }        
        
        if (getCurrentCard().getName().equals("blok123")) {
            System.out.println("if blok 1");
//            EntityManagerFactory emf = Persistence.createEntityManagerFactory("pkl53");
//            Blok123UsahaDao blok123UsahaDao = new Blok123UsahaDao(emf);
//            blok123UsahaDao.setJumlahPekerja(this, blokUsaha1231.getB1Keterangantempat1(), blokUsaha1231);
//            
//            if (blok123UsahaDao.getTerdaftar()) {
//                card.next(getContainerPanel());
//                jScrollPane1.getViewport().setViewPosition(new java.awt.Point(0, 0));
//            }
            setJumlahPekerja(2);
            card.next(getContainerPanel());
            jScrollPane1.getViewport().setViewPosition(new java.awt.Point(0, 0));
            
        } else if (getCurrentCard().getName().equals("blok5d")) {
            //KODING BUAT NAMBAHIN MODEL DISINI
            SubmitAllControllerUsaha setBlok = new SubmitAllControllerUsaha();
            setBlok.setPanelBlok123(blokUsaha1231);
            setBlok.setPanelBlok4(blokUsaha41);
            setBlok.setPanelBlok5(blokUsaha51);
            setBlok.setPanelBlok5b(blokUsaha5B1);
            setBlok.setPanelBlok5c(blokUsaha5C1);
            setBlok.setPanelBlok5d(blokUsaha5D1);
            setBlok.getModelB1();
            
            B3Rinciusaha b3 = setBlok.getModelB3();
            ArrayList<B3r15Pekerja> b3r15List = new ArrayList<B3r15Pekerja>();
            for (B3r15Pekerja b3r15 : blokUsaha5B1.getListModel()) {
                b3r15List.add(b3r15);
            }
            listB3Model.add(b3);
            listAllB3R15.add(b3r15List);
            
            if (loop < getJumlahPekerja()) {
                System.out.println("masuk if if");

//                    kuesionerControllerUsaha.resetBlok3AUsaha();
//                    kuesionerControllerUsaha.resetBlok3BUsaha();
//                    kuesionerControllerUsaha.resetBlok3CUsaha();
//                    kuesionerControllerUsaha.resetBlok3DUsaha();
//                    kuesionerControllerUsaha.resetBlok3EUsaha();
                loop++;
                card.show(getContainerPanel(), "card4");
                blokUsaha51.getB5R1NOART().requestFocusInWindow();
                jScrollPane1.getViewport().setViewPosition(new java.awt.Point(0, 0));
                JOptionPane.showMessageDialog(this, "Lanjut input entri ke-" + loop);
            } else {
                System.out.println("masuk if else");
                lastLoop = true;
                Submit.setContentAreaFilled(true);
                JOptionPane.showMessageDialog(this, "Sudah selesai. Silakan submit");
                
            }
        } else {
            System.out.println("masuk else");
            card.next(getContainerPanel());
            jScrollPane1.getViewport().setViewPosition(new java.awt.Point(0, 0));
        }
    }//GEN-LAST:event_nextButtonActionPerformed

    private void previousButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_previousButtonActionPerformed
        // TODO add your handling code here:
        card = (CardLayout) getContainerPanel().getLayout();
        card.previous(getContainerPanel());
        jScrollPane1.getViewport().setViewPosition(new java.awt.Point(0,0));
    }//GEN-LAST:event_previousButtonActionPerformed

    private void homeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_homeButtonActionPerformed
        // TODO add your handling code here:
//        kuesionerControllerUsaha = new KuesionerControllerUsaha();
//        kuesionerControllerUsaha.resetAll();
        
        card = (CardLayout) getPanel().getLayout();
        card.show(getPanel(), "homePanel");
        
    }//GEN-LAST:event_homeButtonActionPerformed

    private void SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveActionPerformed
        // TODO add your handling code here:
        //BackupKuesionerUsaha.write();
        JOptionPane.showMessageDialog(this, "Save Success");
    }//GEN-LAST:event_SaveActionPerformed

    private void SubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SubmitActionPerformed
        if (!lastLoop) {
            System.out.println("Masuk submit lastLoop false");
            SubmitAllControllerUsaha setBlok = new SubmitAllControllerUsaha();
            setBlok.setPanelBlok123(blokUsaha1231);
            setBlok.setPanelBlok4(blokUsaha41);
            setBlok.setPanelBlok5(blokUsaha51);
            setBlok.setPanelBlok5b(blokUsaha5B1);
            setBlok.setPanelBlok5c(blokUsaha5C1);
            setBlok.setPanelBlok5d(blokUsaha5D1);
            setBlok.getModelB1();
            
            B3Rinciusaha b3 = setBlok.getModelB3();
            ArrayList<B3r15Pekerja> b3r15List = new ArrayList<B3r15Pekerja>();
            for (B3r15Pekerja b3r15 : blokUsaha5B1.getListModel()) {
                b3r15List.add(b3r15);
            }
            listB3Model.add(b3);
            listAllB3R15.add(b3r15List);
        }
        
        SubmitAllControllerUsaha submit = new SubmitAllControllerUsaha();
        submit.setPanelBlok123(blokUsaha1231);
        submit.setPanelBlok4(blokUsaha41);
        submit.setPanelBlok5(blokUsaha51);
        submit.setPanelBlok5b(blokUsaha5B1);
        submit.setPanelBlok5c(blokUsaha5C1);
        submit.setPanelBlok5d(blokUsaha5D1);
//        submit.setB1KeterangantempatModel(blokUsaha1231.getB1Keterangantempat1());
        submit.setListB3Model(listB3Model);
        submit.setListAllB3R15(listAllB3R15);
        submit.insertAllBlok();
        
//        EntityManagerFactory emf = Persistence.createEntityManagerFactory("pkl53");
//        B2UmumusahaJpaController b2UmumusahaJpaController = new B2UmumusahaJpaController(emf);
//        for (B2Umumusaha b2 : blokUsaha41.getListModel()) {
//            try {
//                b2.setKks(new B1Keterangantempat("1111111"));
//                b2.setKKSArt("11111111");
//                b2UmumusahaJpaController.create(b2);
//            } catch (Exception ex) {
//                Logger.getLogger(DataEntriUsahaPanel.class.getName()).log(Level.SEVERE, null, ex);
//            }
//        }
        
        if (submit.isSuccess()) {
            JOptionPane.showMessageDialog(this, "Submit Success!");
        } else {
            JOptionPane.showMessageDialog(this, "Submit Gagal!");
        }
    }//GEN-LAST:event_SubmitActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Save;
    private javax.swing.JButton Submit;
    private pkl53.imageClass.BackgroundLain backgroundLain1;
    private pkl53.view.BlokUsaha123 blokUsaha1231;
    private pkl53.view.BlokUsaha4 blokUsaha41;
    private pkl53.view.BlokUsaha5 blokUsaha51;
    private pkl53.view.BlokUsaha5B blokUsaha5B1;
    private pkl53.view.BlokUsaha5C blokUsaha5C1;
    private pkl53.view.BlokUsaha5D blokUsaha5D1;
    private static javax.swing.JPanel containerPanel;
    private javax.swing.JButton homeButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton nextButton;
    private javax.swing.JButton previousButton;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the containerPanel
     */
    public static javax.swing.JPanel getContainerPanel() {
        return containerPanel;
    }

    /**
     * @return the blokUsaha1231
     */
    public pkl53.view.BlokUsaha123 getBlokUsaha1231() {
        return blokUsaha1231;
    }

    /**
     * @param blokUsaha1231 the blokUsaha1231 to set
     */
    public void setBlokUsaha1231(pkl53.view.BlokUsaha123 blokUsaha1231) {
        this.blokUsaha1231 = blokUsaha1231;
    }

    /**
     * @return the blokUsaha41
     */
    public pkl53.view.BlokUsaha4 getBlokUsaha41() {
        return blokUsaha41;
    }

    /**
     * @param blokUsaha41 the blokUsaha41 to set
     */
    public void setBlokUsaha41(pkl53.view.BlokUsaha4 blokUsaha41) {
        this.blokUsaha41 = blokUsaha41;
    }

    /**
     * @return the blokUsaha51
     */
    public pkl53.view.BlokUsaha5 getBlokUsaha51() {
        return blokUsaha51;
    }

    /**
     * @param blokUsaha51 the blokUsaha51 to set
     */
    public void setBlokUsaha51(pkl53.view.BlokUsaha5 blokUsaha51) {
        this.blokUsaha51 = blokUsaha51;
    }

    /**
     * @return the blokUsaha5B1
     */
    public pkl53.view.BlokUsaha5B getBlokUsaha5B1() {
        return blokUsaha5B1;
    }

    /**
     * @param blokUsaha5B1 the blokUsaha5B1 to set
     */
    public void setBlokUsaha5B1(pkl53.view.BlokUsaha5B blokUsaha5B1) {
        this.blokUsaha5B1 = blokUsaha5B1;
    }

    /**
     * @return the blokUsaha5C1
     */
    public pkl53.view.BlokUsaha5C getBlokUsaha5C1() {
        return blokUsaha5C1;
    }

    /**
     * @param blokUsaha5C1 the blokUsaha5C1 to set
     */
    public void setBlokUsaha5C1(pkl53.view.BlokUsaha5C blokUsaha5C1) {
        this.blokUsaha5C1 = blokUsaha5C1;
    }

    /**
     * @return the blokUsaha5D1
     */
    public pkl53.view.BlokUsaha5D getBlokUsaha5D1() {
        return blokUsaha5D1;
    }

    /**
     * @param blokUsaha5D1 the blokUsaha5D1 to set
     */
    public void setBlokUsaha5D1(pkl53.view.BlokUsaha5D blokUsaha5D1) {
        this.blokUsaha5D1 = blokUsaha5D1;
    }

    /**
     * @return the panel
     */
    public JPanel getPanel() {
        return panel;
    }
    
    private JPanel getCurrentCard() {
        JPanel card = null;
        for (Component comp : containerPanel.getComponents()) {
            if (comp.isVisible() == true) {
                card = (JPanel) comp;
            }
        }
        return card;
    }

    /**
     * @return the jumlahPekerja
     */
    public int getJumlahPekerja() {
        return jumlahPekerja;
    }

    /**
     * @param jumlahPekerja the jumlahPekerja to set
     */
    public void setJumlahPekerja(int jumlahPekerja) {
        this.jumlahPekerja = jumlahPekerja;
    }
    
}
